#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019/12/28 15:08
# @Author : diyhome
# @Site : https://gitee.com/jingjiangxueyuan_hmqs/
# @File : main.py
# @Software: PyCharm

import json
import os

configName = "c_cpp_properties.json"
gccPath = "D:\\ProgramFiles\\mingw64\\x86_64-w64-mingw32\\include\\stdint.h"
TrueStudioInclude = "D:\\Program Files\\TrueStudio\\TrueSTUDIO for STM32 9.3.0\\ARMTools\\arm-atollic-eabi\\include\\**", "D:\\Program Files\\TrueStudio\\TrueSTUDIO for STM32 9.3.0\\ARMTools\\lib\\gcc\\arm-atollic-eabi\\6.3.1\\include\\**", "D:\\Program Files\\TrueStudio\\TrueSTUDIO for STM32 9.3.0\\ARMTools\\lib\\gcc\\arm-atollic-eabi\\6.3.1\\include-fixed\\**",


# 获取所有属于头文件的路径
def getPath():
    pathlist = []
    parent_path = os.path.abspath('..')
    drive_dir_path = os.path.join(parent_path, 'Drivers')
    for root, dirname, files in os.walk(drive_dir_path):
        # 转换数据类型
        _root = listTostr(root)
        _dirname = listTostr(dirname)
        for filename in files:
            if filename.split('.')[-1] == 'h':
                path_tmp = os.path.join(_root, filename)
                pathlist.append(path_tmp)
    return pathlist


def genterJSON():
    article_info = {}
    data = json.loads(json.dumps(article_info))
    data["configurations"] = {"name": "Win32", 'includePath': [], 'defines': [], 'forcedInclude': []}
    # 引入路径
    include_path = ('${workspaceFolder}\\**', '${workspaceFolder}\\Inc\\**') + TrueStudioInclude
    # forcedInclude = tuple(getPath())
    # print(forcedInclude)
    data['configurations']['includePath'] = include_path
    data['configurations']['forcedInclude'] = gccPath

    tmp_list = list(json.dumps(data, ensure_ascii=False))
    tmp_list.insert(19, '[')
    tmp_list.insert(-1, ']')
    return "".join(tmp_list)


def listTostr(tmp=[]):
    return "".join(tmp)


if __name__ == '__main__':
    if os.path.isfile(configName):
        os.remove(configName)
    with open(configName, 'w') as f:
        f.write(genterJSON())
